<template>
  <div class="orderSettle">
    <ul class="query">
      <li class="query-option">
        <span>关键词：</span>
        <el-input size="medium" v-model="queryData.queryCondition" placeholder="请输入订单号、用户ID、商品名称"></el-input>
      </li>
      <li class="query-option">
        <span>下单时间：</span>
        <el-date-picker type="datetimerange" v-model="date" value-format="yyyy-MM-dd HH:mm:ss" @change="changeDate" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" size="medium">
        </el-date-picker>
      </li>
      <li class="query-option">
        <el-button size="medium" type="primary" @click="handleCurrentChange(1)">查询</el-button>
        <el-button size="medium" type="warning" @click="exportExcel">导出</el-button>
      </li>
    </ul>
    <div class="wrapper">
      <el-table class="table" :data="tableData" stripe highlight-current-row border>
        <el-table-column align="center" type="index" label="序号" width="50">
        </el-table-column>
        <el-table-column align="center" prop="orderNo" label="订单号">
        </el-table-column>
        <el-table-column align="center" prop="orderTime" label="下单时间" width="180">
        </el-table-column>
        <el-table-column align="center" prop="buyUnitPrice" label="单价（元）">
        </el-table-column>
        <el-table-column align="center" prop="" label="购买数量 / 单位">
          <template slot-scope="scope">
            <span>{{scope.row.buyCount}}{{scope.row.unit}}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="buyPay" label="支付金额（元）">
        </el-table-column>
        <el-table-column align="center" prop="fee" label="交易手续费（元）">
        </el-table-column>
        <el-table-column align="center" prop="realProceeds" label="实际收款金额（元）">
        </el-table-column>
        <el-table-column align="center" prop="userId" label="用户ID">
        </el-table-column>
        <el-table-column align="center" prop="goodsName" label="商品名称">
        </el-table-column>
        <el-table-column align="center" prop="" label="付款状态">
          <template slot-scope="scope">
            <span>{{scope.row.status==1?'已收款':'待收款'}}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="operation" label="操作">
          <template slot-scope="scope">
            <span class="btn" @click="$router.push({path:'/orderDetail',query:{id:scope.row.id}})">查看</span>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination v-show='page.total>page.pageSize' background layout="total,prev, pager, next,jumper" :page-size="page.pageSize" :total="page.total" :current-page="page.current" @current-change="handleCurrentChange">
      </el-pagination>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      date: [],
      exportData: {},
      queryData: {
        startTime: '',
        endTime: '',
        queryCondition: ''
      },
      tableData: [],
      page: {
        pageSize: 10,
        current: 1,
        total: 0
      }
    };
  },
  mounted() {
    this.getTableData();
  },
  methods: {
    async getTableData() {
      let { data } = await this.$post({
        api: '/cash/getAccountFundsList',
        data: {
          pageSize: this.page.pageSize,
          current: this.page.current,
          startTime: this.queryData.startTime,
          endTime: this.queryData.endTime,
          queryCondition: this.queryData.queryCondition
        }
      });
      if (data.code === 0) {
        this.tableData = data.data;
        this.page.total = data.rdPage.total;
        this.exportData = Object.assign({}, this.queryData);
      } else {
        this.$message({
          type: 'error',
          message: data.msg
        });
      }
    },
    handleCurrentChange(val) {
      this.page.current = val;
      this.getTableData();
    },
    changeDate(val) {
      if (val) {
        this.queryData.startTime = val[0];
        this.queryData.endTime = val[1];
      } else {
        this.queryData.startTime = '';
        this.queryData.endTime = '';
      }
    },
    exportExcel() {
      if (!this.tableData.length) {
        this.$message({
          type: 'error',
          message: '不能导出数据为空的excel'
        });
      } else {
        this.$confirm('确认进行导出操作吗？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$post({
            api: '/import/AccountFundsList',
            data: this.exportData
          }).then(({ data }) => {
            if (data.code === 0) {
              let link = document.createElement('a');
              link.href = url + data.data.replace('static/', '');
              link.download = 'order';
              link.click();
            } else {
              this.$message({
                type: 'error',
                message: data.msg
              });
            }
          });
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消操作'
          });
        });
      }
    }
  }
};
</script>

<style lang="scss" scoped>
.query {
  overflow: hidden;
  padding: 15px 30px;
  border-bottom: 1px solid #eeeeee;
  &-option {
    float: left;
    margin-right: 50px;
    &:last-child {
      margin-right: 0;
    }
  }
  .el-input {
    width: 300px;
  }
}
.wrapper {
  padding: 0 15px;
  .btn {
    color: #0033ff;
    margin: 0 5px;
    cursor: pointer;
  }
}
</style>
